package testcases;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.awt.Color;
import java.awt.Dimension;
import java.util.Date;

import javax.swing.JFormattedTextField;

import juf.FieldProperty;
import juf.MethodProperty;
import juf.adapter.Adapter;
import juf.adapter.AdapterFactory;
import junit.framework.TestCase;

import org.junit.Test;
/**
 * Tests the annotations
 * 
 * TODO write tests for all the annotations we explicitly use (like the name, or the custom adapter)
 * 
 * @author Parul Sharma
 *
 */
public class AnnotationsTest extends TestCase {
	
	@Test
	public void testCreateAdapter_Annotations() {
				
		Student fixture = new Student(007,"Jane","Somebody", new Date(), false, 20);
		
		try {
			MethodProperty mp;
			
			//check if the Name annotation is present
			//TODO: use reflection to get the private panel of an adapter
			//and then the label - maybe use invoke?
			
			mp = new MethodProperty(fixture.getClass().getMethod("getFine"),fixture.getClass().getMethod("setFine",double.class), fixture);
			Adapter a = AdapterFactory.createAdapter(mp);
			
			JFormattedTextField component =  (JFormattedTextField) a.getDataComponent();		
			//check that the change specified has been made
			assertEquals((component.getForeground().equals(Color.RED)),true);
			
			
			//check if the colour property was set
			mp = new MethodProperty (fixture.getClass().getMethod("getStudentId"), fixture);
			Adapter a2 = AdapterFactory.createAdapter(mp);
			JFormattedTextField component2 =  (JFormattedTextField) a2.getDataComponent();
			//check that columns annotation was used
			assertEquals((component2.getColumns()==5),true);	
			//check that no other annotation was used
			assertEquals((component2.getForeground().equals(Color.RED)),false);
			
		
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			fail("unhandled exception" + e.toString());
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			fail("unhandled exception" + e.toString());
		}
	} //end testCreateAdapter_Annotations()
	

	public void testAttributeEnable(){
		College fixture = new College("Massey Engineering","Ag Hort",new Date(), 15000.00);
		
		MethodProperty mp;
		try {
			mp = new MethodProperty(fixture.getClass().getMethod("getBudget"),fixture.getClass().getMethod("setBudget",double.class), fixture);
			Adapter a = AdapterFactory.createAdapter(mp);
			JFormattedTextField component =  (JFormattedTextField) a.getDataComponent();
			assertEquals((component.isEnabled()),false);			
		
		} catch (SecurityException e) {
			fail("unhandeled exception");
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			fail("unhandeled exception");
			e.printStackTrace();
		}
	}
	
	public void testAttributeAutoScroll(){
		College fixture = new College("Massey Engineering","Ag Hort",new Date(), 15000.00);
		try {
			FieldProperty fp = new FieldProperty(fixture.getClass().getField("noOfDepartments"),fixture);
			Adapter a = AdapterFactory.createAdapter(fp);
			JFormattedTextField component =  (JFormattedTextField) a.getDataComponent();
			assertEquals(component.getAutoscrolls(), true);
		
		} catch (SecurityException e) {
			fail("unhandeled exception");
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			fail("unhandeled exception");
			e.printStackTrace();
		}
	}
	
	public void testAttributeVisible(){
		try {
			College fixture = new College("Massey Engineering","Ag Hort",new Date(), 15000.00);
			FieldProperty fp = new FieldProperty(fixture.getClass().getField("id"),fixture);
			Adapter a = AdapterFactory.createAdapter(fp);
			JFormattedTextField component =  (JFormattedTextField) a.getDataComponent();
			assertEquals(component.isVisible(), false);
		} catch (SecurityException e) {
			fail("unhandeled exception");
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			fail("unhandeled exception");
			e.printStackTrace();
		}
	
	}
	
	public void testAttributeSize(){
		College fixture = new College("Massey Engineering","Ag Hort",new Date(), 15000.00);
		
		MethodProperty mp;
		try {
			mp = new MethodProperty(fixture.getClass().getMethod("getBudget"),fixture.getClass().getMethod("setBudget",double.class), fixture);
			Adapter a = AdapterFactory.createAdapter(mp);
			JFormattedTextField component =  (JFormattedTextField) a.getDataComponent();
			assertEquals((component.getSize()), new Dimension(20, 15));			
	
		} catch (SecurityException e) {
			fail("unhandeled exception");
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			fail("unhandeled exception");
			e.printStackTrace();
		}
		
	}

	public void testAttributeColumn(){
		College fixture = new College("Massey Engineering","Ag Hort",new Date(), 15000.00);
		
		MethodProperty mp;
		try {
			mp = new MethodProperty(fixture.getClass().getMethod("getLocation"),fixture.getClass().getMethod("setLocation",String.class), fixture);
			Adapter a = AdapterFactory.createAdapter(mp);
			JFormattedTextField component =  (JFormattedTextField) a.getDataComponent();
			assertEquals((component.getColumns()), 10);			
	
		} catch (SecurityException e) {
			fail("unhandeled exception");
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			fail("unhandeled exception");
			e.printStackTrace();
		}	
	}
	
	public void testAttributeForeground(){
		College fixture = new College("Massey Engineering","Ag Hort",new Date(), 15000.00);
		
		MethodProperty mp;
		try {
			mp = new MethodProperty(fixture.getClass().getMethod("getLocation"),fixture.getClass().getMethod("setLocation",String.class), fixture);
			Adapter a = AdapterFactory.createAdapter(mp);
			JFormattedTextField component =  (JFormattedTextField) a.getDataComponent();
			assertEquals((component.getForeground()), Color.red);			
	
		} catch (SecurityException e) {
			fail("unhandeled exception");
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			fail("unhandeled exception");
			e.printStackTrace();
		}
		
	}

	
}

